package com.oyjd;

import java.io.File;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.util.List;


public class DownBook {
	public static String url = "http://www.i7wu.cn";
	public static String url1 = "http://www.i7wu.cn/bang/xiazai-0-1.htm";
	public static String reg1 = "<div class=\"ranktitle\">.*?<a target=\"_blank\" href=\"(.*?)\">";

	public static String reg21 = "<a href=\"#\"><img src=\"(.*?)\".*?name=\"ImageUrl\" id=\"ImageUrl\"\\s/>";
	public static String reg22 = "<div id=\"bookTitle\">.*?<h1>(.*?)</h1>";
	public static String reg23 = "<span class=\"smalltext\">作者：(.*?)</span>";
	public static String reg24 = "<a href=\"#\">内容简介</a>.*?<div class=\"intro\">(.*?)</div>";
	public static String reg25 = "<div class=\"down1\"><a href=\"(.*?)\".*?</div>";

	public static String sql = "insert into T_BOOK (BOOK_ID, AUTHOR, DESCRIPTION, FORMAT, ICON_PATH, NAME, SOU_PATH, SPACE_SIZE, CREATOR, CREATE_TIME, LAST_EDITOR, LAST_UPDATE_TIME, STATUS) values (''{0}'', ''{1}'', ''{2}'', ''{3}'', ''{4}'', ''{5}'', ''{6}'', {7}, ''admin'', now(), ''admin'', now(), 0);";
	public static String hot_sql = "insert into T_HOT_SOURCE (DOWN_COUNT, SOU_ID, SOU_TYPE, ID) values ({0}, ''{1}'', ''{2}'', ''{3}'');";
	public static String order_sql = "insert into T_BOOK_TYPE_ORDER (ORDER_KEY, BOOK_ID, TYPE, ID) values ({0}, ''{1}'', ''{2}'', ''{3}'');";

	public static String souPath = "N:/SL/trunk/book/";
	public static String sqlPath = "N:/SL/trunk/script/book.sql";

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		HttpNet.setRequestProperty("Referer", url);
		HttpNet hn = new HttpNet();
		String c = new String(hn.getBytesFromNet(url1), "gb2312");
		final List<String> tmp = RegexUtil.getMuti(c, reg1);

		final StringBuffer sb = new StringBuffer("use sldb;\r\n\r\n");
		final StringBuffer sb1 = new StringBuffer("\r\n#==============================\r\n");
		final StringBuffer sb2 = new StringBuffer("\r\n#==============================\r\n");

		new MyTask<String>(10, tmp, new MyTaskLogic<String>() {
			private HttpNet hn = new HttpNet();

			public void doLogic(int count, int pos, String source) {
				String detailUrl = url + source;
				try {
					String c1 = new String(hn.getBytesFromNet(detailUrl), "gb2312");
					String imgUrl = RegexUtil.getSingle(c1, reg21);
					String title = RegexUtil.getSingle(c1, reg22);
					String author = RegexUtil.getSingle(c1, reg23);
					String desc = RegexUtil.getSingle(c1, reg24);
					String souUrl = RegexUtil.getSingle(c1, reg25);
					int a = souUrl.lastIndexOf("/");
					souUrl = souUrl.substring(0, a + 1) + URLEncoder.encode(souUrl.substring(a + 1), "UTF-8");

					String faceName = downSou(hn, imgUrl, imgUrl, "sl_face");
					String rarName = downSou(hn, souUrl, souUrl, "sl_rar");
					String bookName = rarName.substring(0, rarName.lastIndexOf(".")) + ".txt";
					File bFile = new File(souPath + bookName);
					if (!bFile.exists()) {
						//RarUtil.exRarForBook(souPath + rarName, souPath, souPath + bookName);
					}
					String len = Long.toString(new File(souPath + bookName).length());
					//
					String souId = FileOP.getUUID();
					String newSql = MessageFormat.format(sql, souId, author, desc, "txt", faceName, title, bookName, len);
					String newHotSql = MessageFormat.format(hot_sql, count--, souId, "TYPE_BOOK", FileOP.getUUID());
					String typeSql = MessageFormat.format(order_sql, pos + 1, souId, "Book_Boutique", FileOP.getUUID());

					sb.append(newSql).append("\r\n");
					sb1.append(newHotSql).append("\r\n");
					sb2.append(typeSql).append("\r\n");
				} catch (Exception err) {
					err.printStackTrace();
					System.out.println(pos + "--error------>" + detailUrl);
				}
				System.out.println(pos + "--finish------>" + detailUrl);

			}

			public void finishAll(List<String> s) {
				try {
					FileOP.writeFile(sqlPath, sb.toString() + sb1.toString() + sb2.toString(), "gb2312");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("**********程序结束************");
			}
		}).start();

	}

	public static String downSou(HttpNet hn, String url, String oldName, String pre) {
		String newName = FileOP.getNewHashName(oldName, pre);
		File file = new File(souPath + newName);
		if (file.exists() && file.length() == 0) {
			file.delete();
		}
		if (!file.exists()) {
			try {
				if (!url.startsWith("http")) {
					url = DownBook.url + url;
				}
				hn.downFile(url, null, souPath + newName);

				if (!new File(souPath + newName).exists()) {
					System.out.println("无效链接----->" + url);
				}
			} catch (Exception e) {
				System.out.println("异常链接----->" + url);
			}
		}
		return newName;
	}
}
